home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / mathstud.zip / SINW.M < prev    next >
Text File  |  1993-03-23  |  1KB  |  63 lines

  1. function y=sinw(npoints, f0, df, ph0)
  2. % y=sinw(num_of_points, num_of_cycles, delta_cycles, initial_phase)
  3. % create frequency hopping sine wave composed of segements of linear
  4. % FM sine waves
  5. %
  6. % num_of_points - number of points in each segment
  7. % f0            - inital frequency
  8. % df            - frequency deviation
  9. % ph0           - initial phase
  10. % Example: sinw(256, 2, 3) generates a linear FM signal
  11.  
  12. %       S.Halevy 7/31/92
  13. %       Copyright (c) 1992 by the MathWizards
  14.  
  15. if nargin < 1
  16.   error('insufficient arguments')
  17. end
  18. npoints=npoints(:);   % make sure it is a col vector
  19. [mp,np]=size(npoints);
  20.  
  21. if nargin < 4
  22.    ph0=zeros(mp,np);  % zero initial phase - radians
  23. else
  24.    ph0=ph0(:);
  25.    [m0,n0]=size(ph0);
  26.    if m0 ~= mp | n0 ~= np
  27.       error('initial phase - dimension error')
  28.    end
  29. end
  30.  
  31. if nargin < 3
  32.    df=zeros(mp,np);   % no frequency deviation
  33. else
  34.    df=df(:);
  35.    [m0,n0]=size(df);
  36.    if m0 ~= mp | n0 ~= np
  37.       error('frequency deviation - dimension error')
  38.    end
  39. end
  40.  
  41. if nargin < 2
  42.    f0=ones(mp,np);    % single cycle
  43. else
  44.    f0=f0(:);
  45.    [m0,n0]=size(f0);
  46.    if m0 ~= mp | n0 ~= np
  47.       error('cycles - dimension error')
  48.    end
  49. end
  50.  
  51. y=[];
  52. pi2=atan(1.0)*8;
  53. for i1=1:mp
  54.    n = npoints(i1);
  55.    t = (0:(n-1))/n;                  % time
  56.    ph1 = (f0(i1)+0.5*df(i1)*t).*t;   % phase
  57.    ph1 -= floor(ph1);                % reduce roundoff errors
  58.  
  59.    y=[y sin(pi2*ph1+ph0(i1))];
  60. end
  61.